<template>
  <div class="select-template-list">
    <el-form :inline="true" style="height: 50px; float: right" size="small">
      <el-form-item>
        <el-input
          v-model.trim="searchForm.reportTitle"
          placeholder="请输入模板名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model.trim="searchForm.reportName"
          placeholder="请输入模板英文标识"
        ></el-input>
      </el-form-item>
      <el-button size="medium" class="btn-icon" @click="getReportList">
        <span class="el-icon-search" />
      </el-button>
      <el-button class="btn-icon" size="medium" @click="refreshSearch">
        <span class="el-icon-refresh" />
      </el-button>
    </el-form>

    <el-table
      v-loading="isLoading"
      :data="reportDatas"
      border
      style="width: 100%"
    >
      <el-table-column prop="title" label="模板名称">
        <template slot-scope="scope">
          <span
            style="cursor: pointer"
            @click="checkTempalate(scope.row.chartLayoutName)"
          >
            {{ scope.row.title }}
          </span>
        </template>
      </el-table-column>

      <el-table-column
        prop="chartLayoutName"
        label="模板标识"
      ></el-table-column>

      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button
            @click="checkTempalate(scope.row.chartLayoutName)"
            type="text"
            size="small"
            >选择</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="pageInfo.currentPage"
      :page-sizes="[5, 10, 20, 50]"
      v-model:page-size="pageInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      v-model:total="pageInfo.totalRecord"
      style="margin-top: 5px"
      @size-change="sizeChange"
      @current-change="currentChange"
    />
  </div>
</template>

<script>
import nt from '../../../network'

export default {
  name: 'selectTemplateList',
  data() {
    return {
      isLoading: false,
      searchForm: {
        reportTitle: '',
        reportName: '',
        chtlytTmpltTypeId: 0,
      },
      reportDatas: [], //模板列表数据
      pageInfo: {
        currentPage: 1,
        pageSize: 10,
        totalRecord: 0,
      },
    }
  },
  methods: {
    checkTempalate(chartLayoutName) {
      this.$emit( 'checkTempalate', chartLayoutName)
    },
    refreshSearch() {
      this.searchForm.reportTitle = ''
      this.searchForm.reportName = ''
      this.pageInfo = {
        currentPage: 1,
        pageSize: 10,
        totalRecord: 0,
      }
      this.getReportList()
    },

    sizeChange(pageSize) {
      this.pageInfo.currentPage = 1
      this.pageInfo.pageSize = pageSize
      this.getReportList()
    },

    currentChange(currentPage) {
      this.pageInfo.currentPage = currentPage
      this.getReportList()
    },

    getReportList() {
      this.isLoading = true
      nt.doRequest({
        method: 'post',
        uri: '/paas/chart-layout/v1/chart/list-all',
        data: {
          params: {
            chartLayoutName: this.searchForm.reportName,
            title: this.searchForm.reportTitle,
            isTemplate: 1,
            chtlytTmpltTypeId: this.searchForm.chtlytTmpltTypeId,
          },
          pageInfo: this.pageInfo,
        },
        onSuccess: (resp) => {
          this.reportDatas = resp.data.content.list
          this.pageInfo = resp.data.content.pageInfo
          this.isLoading = false
        },
        onError: (err) => {
          this.isLoading = false
        },
      })
    },
  },
  mounted() {
    this.getReportList()
  },
  emits: ['checkTempalate'],
}
</script>

<style lang="scss" scoped>
.select-template-list {
  margin: 10px;
}
</style>
